<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="elibrary.*"%>
<%@ page language="java" import="java.util.*"%>
<%@ include file="config.jsp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>My-Library | Make Reservation</title>
    </head>
    <body>
        <%
            try
            {
    	
                //user id is fetched from the session and if a valid user is not logged in then he is sent to the login page
    	
                String uid=(String)session.getAttribute("uid");
                if(uid==null||uid.equals(""))
                {
                        response.sendRedirect("index.jsp");
                }
    	
                //user id is decrypted
    	
                DesEncrypter encrypter = new DesEncrypter();
                        int uid1 = Integer.parseInt(encrypter.decrypt(uid));
                Connection con=DaoConnection.getcon(connectURL, DBuser, DBpass);
                Statement st=con.createStatement();
                ResultSet rs=st.executeQuery("SELECT * FROM USERS WHERE UID="+uid1);
                rs.next();
                String bookid = request.getParameter("id");
    	
    	
    	
                //check if you have already issued this book
                String query = "SELECT * FROM COPY WHERE ISSUED = 1 UID ="+uid1+" AND BOOKID = '"+bookid+"'";
                        PreparedStatement ps=con.prepareStatement(query);
                        ResultSet check = ps.executeQuery();
                        if(check.next()){
                                session.setAttribute("message", "You have already have a copy of this book. No new reservation made.");
                                response.sendRedirect("viewBook.jsp?id="+bookid);
                        }
		
                //check if you already have a reservation
                query = "SELECT * FROM RESERVATION WHERE UID ="+uid1+" AND BOOKID = '"+bookid+"'";
                        ps=con.prepareStatement(query);
                        check = ps.executeQuery();
                        if(check.next()){
                                session.setAttribute("message", "You have already made a reservation for this book. No new reservation made.");
                                response.sendRedirect("viewBook.jsp?id="+bookid);
                        }
		
                //make reservation
                        query = "INSERT INTO RESERVATION (UID, BOOKID, RESVDATE) VALUES("+uid1+",'"+bookid+"',CURRENT TIMESTAMP)";
                        ps=con.prepareStatement(query);
                        int res=ps.executeUpdate();
		
		
                        //get details of book
                        query = "SELECT * FROM BOOK WHERE BOOKID = '"+bookid+"'";
                        ps=con.prepareStatement(query);
                        ResultSet bookDetail = ps.executeQuery();
                        bookDetail.next();
		
                        //send mail to the person making reservation.
                        String to = rs.getString("EMAIL");
                        String from = siteEmail;
                        String subject = "New book reservation details";
                        String text = "Dear user,\nWe have received a request for reservation of a book from you.";
                        text = text + "\n\nBook Details:";
                        text = text + "\nBOOK ID: "+bookid;
                        text = text + "\nBOOK NAME: "+bookDetail.getString("BOOKNAME");
                        text = text + "\nAUTHOR: "+bookDetail.getString("AUTHOR");
                        text = text + "\nPUBLISHER: "+bookDetail.getString("PUBLISHER");
		
                        Mailer mailer = new Mailer();
                        mailer.sendmail(to, from, subject, text);
		
                        //send mails to everyone who have a copy of the book
                        query = "SELECT * FROM COPY WHERE ISSUED = 1 AND BOOKID = '"+bookid+"'";
                        ps=con.prepareStatement(query);
                        ResultSet users = ps.executeQuery();
                        while(users.next()){
                                query = "SELECT * FROM USERS WHERE UID ="+users.getInt("UID");
                                ps=con.prepareStatement(query);
                                ResultSet usr = ps.executeQuery();
                                usr.next();
                                to = usr.getString("EMAIL");
                                subject = "Reservation of a book held by you";
                                text = "Dear user,\nWe have received a request for reservation for a book which is with you.";
                                text = text + "\n\nBook Details:";
                                text = text + "\nBOOK ID: "+bookid;
                                text = text + "\nBOOK NAME: "+bookDetail.getString("BOOKNAME");
                                text = text + "\nAUTHOR: "+bookDetail.getString("AUTHOR");
                                text = text + "\nPUBLISHER: "+bookDetail.getString("PUBLISHER");
                                text = text + "\n\nIf you do not need this book any more please return it.";
			
                                mailer.sendmail(to, from, subject, text);
                        }
                        session.setAttribute("message", "Reservation made succesfully.");
                        response.sendRedirect("viewBook.jsp?id="+bookid);
                }catch(Exception e){
                        e.printStackTrace();
                        out.println(e.getMessage());
                }
        %>

    </body>
</html>
